package com.lyb.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.lyb.spzx.manager.service.ProductService;
import com.lyb.spzx.model.dto.product.ProductDto;
import com.lyb.spzx.model.entity.product.Product;
import com.lyb.spzx.model.vo.common.Result;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商品列表管理接口")
@RestController
@RequestMapping("admin/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Operation(summary = "分页查找")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageInfo<Product>> findAllProductByPage(@PathVariable Integer pageNum,
                                                          @PathVariable Integer pageSize,
                                                          ProductDto productDto){
        PageInfo<Product> productPageInfo = productService.findAllProductByPage(pageNum,pageSize,productDto);
        return Result.build(productPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加商品")
    @PostMapping("/saveProduct")
    public Result saveProduct(@RequestBody Product product){
        productService.saveProduct(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id查询商品信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Product product = productService.getById(id);
        return Result.build(product,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "保存修改信息")
    @PutMapping("/updateProduct")
    public Result updateById(@RequestBody Product product){
        productService.updateById(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除商品信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        productService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "审核商品")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus){

        productService.updateAuditStatus(id,auditStatus);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "商品上下架")
    @GetMapping("/updateStatus/{id}/{tatus}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status){

        productService.updateStatus(id,status);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
